package com.hyf.controller;

import com.github.pagehelper.PageInfo;
import com.hyf.entity.R;
import com.hyf.pojo.Brand;
import com.hyf.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@SuppressWarnings("all")
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    public Object findAll() {
        List<Brand> list = brandService.findAll();
        return new R<>(list);
    }

    @GetMapping("{id}")
    public Object findById(@PathVariable("id") Integer id) {
        Brand brand = brandService.findById(id);
        return new R<>(brand);
    }

    @PostMapping
    public Object insert(@RequestBody Brand brand) {
        brandService.add(brand);
        return new R<>();
    }

    @DeleteMapping("{id}")
    public Object deleteById(@PathVariable("id") Integer id) {
        brandService.deleteById(id);
        return new R<>();
    }

    @PutMapping
    public Object update(@RequestBody Brand brand) {
        brandService.update(brand);
        return new R<>();
    }

    @GetMapping("search")
    public Object findByName(@RequestBody Brand brand) {
        List<Brand> brandList = brandService.findByName(brand);
        return new R<>(brandList);
    }

    @GetMapping("{page}/{pageSize}")
    public Object findByPage(@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
        PageInfo<Brand> pageInfo = brandService.findByPage(page, pageSize);
        return new R<>(pageInfo);
    }

    @GetMapping("search/{page}/{pageSize}")
    public Object findPageByName(@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize, @RequestBody Brand brand) {
        PageInfo<Brand> pageInfo = brandService.findPageByName(page, pageSize, brand);
        return new R<>(pageInfo);
    }

}
